package com.visualexpress.shards;

import java.util.List;

import org.hibernate.shards.ShardId;
import org.hibernate.shards.loadbalance.RoundRobinShardLoadBalancer;
import org.hibernate.shards.strategy.ShardStrategy;
import org.hibernate.shards.strategy.ShardStrategyFactory;
import org.hibernate.shards.strategy.ShardStrategyImpl;
import org.hibernate.shards.strategy.access.SequentialShardAccessStrategy;
import org.hibernate.shards.strategy.access.ShardAccessStrategy;
import org.hibernate.shards.strategy.resolution.AllShardsShardResolutionStrategy;
import org.hibernate.shards.strategy.resolution.ShardResolutionStrategy;
import org.hibernate.shards.strategy.selection.RoundRobinShardSelectionStrategy;
import org.hibernate.shards.strategy.selection.ShardSelectionStrategy;



public  class VEShardStrategyFactory implements ShardStrategyFactory {
	ShardSelectionStrategy selectionStrategy;
	ShardResolutionStrategy resolutionStrategy;
	ShardAccessStrategy accessStrategy;
	
	
	

	public ShardSelectionStrategy getSelectionStrategy(List<ShardId> shardIds) {
		  if(this.selectionStrategy ==null)
		  {
			  RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds);
			  this.selectionStrategy = new RoundRobinShardSelectionStrategy(loadBalancer);
		  }
		  return selectionStrategy;
	}




	public ShardResolutionStrategy getResolutionStrategy(List<ShardId> shardIds) {
		if(this.resolutionStrategy ==null)
			{this.resolutionStrategy = new AllShardsShardResolutionStrategy(shardIds);
		
			}
		return resolutionStrategy;
	}




	public ShardAccessStrategy getAccessStrategy(List<ShardId> shardIds) {
		if(this.accessStrategy ==null)
			this.accessStrategy = new SequentialShardAccessStrategy();
		return accessStrategy;
	}




	@Override
	public ShardStrategy newShardStrategy(List<ShardId> shardIds) {
		
		   return new ShardStrategyImpl(this.getSelectionStrategy(shardIds), this.getResolutionStrategy(shardIds), this.getAccessStrategy(shardIds));

	}
	
	
}
